package sk.fiit.icp.components;

import org.apache.wicket.authroles.authentication.AuthenticatedWebSession;
import org.apache.wicket.authroles.authorization.strategies.role.Roles;
import org.apache.wicket.injection.Injector;
import org.apache.wicket.request.Request;
import org.apache.wicket.spring.injection.annot.SpringBean;

import sk.fiit.icp.data.Users;
import sk.fiit.icp.data.UsersDao;
import sk.fiit.icp.service.SessionService;

public class SignInSession extends AuthenticatedWebSession {
	private static final long serialVersionUID = 1L;

	@SpringBean(name = "usersDao")
	UsersDao usersDao;
	
	@SpringBean(name = "sessionService")
	SessionService sessionService;

	Users user = null;

	public SignInSession(Request request) {
		super(request);
	
		Injector.get().inject(this);
	}

	@Override
	public boolean authenticate(final String username, final String password) {
		user = usersDao.findByEmailAndPassword(username, password);
		if (user != null) {
			sessionService.setUsers(user);
			return true;
		} else {
			return false;
		}
	}

	@Override
	public Roles getRoles() {
		if (isSignedIn()) {
			// If the user is signed in, they have these roles
			return new Roles(Roles.ADMIN);
		}
		return null;
	}
	public void setUser(Users user) {
		this.user = user;
	}

	public Users getUser() {
		if (isSignedIn()) {
			return this.user;
		}
		return null;
	}
	
}